package com.ayoka.parenting.ui.meditation.detail;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.MeditationDetailBean;
import com.ayoka.parenting.http.API;
import com.ayoka.parenting.http.HttpBeanInterceptCallback;
import com.ayoka.parenting.interf.CallbackUtil;
import com.ayoka.parenting.interf.RequestCallback;
import com.ayoka.parenting.mvp.BasePresenter;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MeditationDetailModel&.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MeditationDetailModel implements MeditationDetailContract.Model{

    @Override
    public void getMeditationDetailData(final BasePresenter presenter, final String meditationId, final RequestCallback<MeditationDetailBean> callback) {
        API.getMeditationDetailData(presenter, meditationId, new HttpBeanInterceptCallback<BaseResponse<MeditationDetailBean>>() {
            @Override
            public void onSuccess(BaseResponse<MeditationDetailBean> response) {
                CallbackUtil.notifySuccess(callback, response.data);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    getMeditationDetailData(presenter, meditationId, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }

    @Override
    public void updateCompletedStatus(final BasePresenter presenter, final String recordId, final int playingDuration, final RequestCallback<BaseResponse> callback) {
        API.updateCompletedStatus(presenter, recordId, playingDuration, new HttpBeanInterceptCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                CallbackUtil.notifySuccess(callback, response);
            }

            @Override
            public void onFailure(int errorCode, String errorMessage) {
                if (errorCode == Constants.WX_ERROR_TOKEN) {
                    updateCompletedStatus(presenter, recordId, playingDuration, callback);
                    return;
                }
                CallbackUtil.notifyFailure(callback, errorCode, errorMessage);
            }
        });
    }
}
